//
//  WSMBaseViewController.m
//  WinesMall
//
//  Created by Isaac on 17/8/29.
//  Copyright © 2017年 Isaac. All rights reserved.
//

#import "WSMBaseViewController.h"
#import "WSMBaseNavigationController.h"

@interface WSMBaseViewController ()

@end

@implementation WSMBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationLeftButton];
    self.view.backgroundColor = kBackGColor;
}

- (void)setNavigationLeftButton {
    
    NSArray *arr = self.navigationController.childViewControllers;
    
    NSString *nowStr = NSStringFromClass([self class]);
    UIViewController *vc = [arr firstObject];
    NSString *str = NSStringFromClass([vc class]);
    
    if (arr.count>1 && ![str isEqualToString:nowStr]) {
        

        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [btn sizeThatFits:CGSizeMake(25, 25)];
//        btn.frame = CGRectMake(0, 0, 20, 20);
//        [btn setBackgroundColor:kBackGColor];
        [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, 40,40);
//        [btn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];

        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        self.navigationItem.leftBarButtonItem = item;
    }

}


-(void)leftButtonClick {
    [self.view endEditing:YES];
    NSInteger n = [self.navigationController.childViewControllers count];
    if (n>1)
    {
        //push
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        //present
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


//增加 titleView
- (void)addTitleViewWithTitle:(NSString *)title {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text=title;
    titleLabel.font=[UIFont systemFontOfSize:17.0f];
    titleLabel.textColor = kTextColorOne;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    //设置titleView
    self.navigationItem.titleView = titleLabel;
    
}
//增加 左右按钮
-(void)addBarButtonItemWithFrame:(CGRect)frame WithTitle:(NSString *)title WithImage:(UIImage *)image target:(id)target action:(SEL)action isLeft:(BOOL)isLeft{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setImage:image forState:UIControlStateNormal];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    if (title.length > 0) {
        [button setTitle:title forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        if (isLeft == NO) {
            [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
        }
        button.titleLabel.textAlignment = NSTextAlignmentRight;
        [button setTitleColor:kTextColorTwo forState:UIControlStateNormal];
        
    }
    //    button.backgroundColor = [UIColor blueColor];
   
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    if (isLeft) {
        self.navigationItem.leftBarButtonItem = item;
    }else {
        self.navigationItem.rightBarButtonItem = item;
    }
}


- (void)gotoSuperViewController {
    if ([self.navigationController.viewControllers count] > 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        [self dismissViewControllerAnimated:YES
                                 completion:^{
                                     
                                 }];
    }
}

- (void)gotoRootController {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)pushViewController:(WSMBaseViewController *)_viewC animate:(BOOL)animated {
    [_viewC setHidesBottomBarWhenPushed:YES];
    
    [self.navigationController pushViewController:_viewC
                                         animated:animated];
    
}

- (void)presentViewController:(WSMBaseViewController *)_viewC animate:(BOOL)animated completion:(void(^)())completion{
    WSMBaseNavigationController *nav = [[WSMBaseNavigationController alloc]initWithRootViewController:_viewC];
    
    [self.navigationController presentViewController:nav
                                            animated:animated
                                          completion:completion];
}

- (void)poptoViewcontroller:(WSMBaseViewController *)_viewC animated:(BOOL)animated {
    
    [self.navigationController popToViewController:_viewC animated:animated];
}


- (void)dealloc {
    NSLog(@"%@---dealloc",NSStringFromClass([self class]));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
